package com.gking.resourcePool.controller;


import com.gking.resourcePool.common.Result;
import com.gking.resourcePool.entity.ProjectGroup;
import com.gking.resourcePool.service.ProjectGroup.ProjectGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/projectGroup")
public class ProjectGroupController {

    @Autowired
    private ProjectGroupService projectGroupService;

    /**
     * 添加项目分组
     *
     * @param projectGroup 项目分组信息
     * @return s
     */
    @RequiresPermissions("RBS:projectGroup:add")
    @PostMapping("/add")
    public Result<String> addProjectGroup(@RequestBody ProjectGroup projectGroup) {
        String name = projectGroup.getName();
        String description = projectGroup.getDescription();
        String projectId = projectGroup.getProjectId();
        if (name.isEmpty() || projectId.isEmpty()) return Result.error("参数不完整");

        try {
            return projectGroupService.addProjectGroup(name, description, projectId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改项目分组
     *
     * @param projectGroup 项目分组信息
     * @return s
     */
    @RequiresPermissions("RBS:projectGroup:update")
    @PostMapping("/update")
    public Result<String> modifyProjectGroup(@RequestBody ProjectGroup projectGroup) {
        String id = projectGroup.getId();
        String name = projectGroup.getName();
        String description = projectGroup.getDescription();
        if (id.isEmpty() || name.isEmpty()) return Result.error("参数不完整");
        try {
            return projectGroupService.modifyProjectGroup(id, name, description);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除项目分组
     *
     * @param projectGroupId 项目分组id
     * @return s
     */
    @RequiresPermissions("RBS:projectGroup:delete")
    @PostMapping("/delete")
    public Result<String> deleteProjectGroup(@RequestParam String projectGroupId) {
        if (projectGroupId.isEmpty()) return Result.error("参数不完整");
        try {
            return projectGroupService.deleteProjectGroup(projectGroupId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取项目分组列表
     *
     * @param projectId 项目id
     * @return s
     */
    @RequiresPermissions("RBS:projectGroup:get")
    @GetMapping("/get")
    public Result<List<ProjectGroup>> getProjectGroupList(@RequestParam String projectId) {
        if (projectId.isEmpty()) return Result.error("参数不完整");
        try {
            return projectGroupService.getProjectGroupList(projectId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
